package com.event.management.frontend.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.event.management.frontend.dto.UpdatePasswordParam;
import com.event.management.frontend.dto.UpdateUserParam;
import com.event.management.frontend.dto.UserDetailDTO;
import com.event.management.frontend.service.FrontUsersService;
import com.event.management.frontend.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 前台用户控制器
 */
@Api(tags = "前台用户接口")
@RestController
@RequestMapping("/api/front/user")
@Slf4j
public class FrontUserController {

    @Autowired
    private FrontUsersService frontUsersService;

    /**
     * 用户资料更新
     * @param updateUserParam 用户资料更新参数
     * @return 更新结果
     */
    @ApiOperation(value = "更新用户资料", notes = "更新当前登录用户的资料")
    @PostMapping("/update")
    @SaCheckLogin
    public Result<Object> updateUserInfo(@RequestBody @Validated UpdateUserParam updateUserParam) {
        log.info("用户资料更新请求");
        
        try {
            boolean result = frontUsersService.updateUserInfo(updateUserParam);
            if (result) {
                return Result.ok(null, "资料更新成功");
            } else {
                return Result.fail("资料更新失败，请稍后重试");
            }
        } catch (Exception e) {
            log.error("用户资料更新失败: {}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 修改密码
     * @param updatePasswordParam 密码修改参数
     * @return 修改结果
     */
    @ApiOperation(value = "修改密码", notes = "修改当前登录用户的密码")
    @PostMapping("/password")
    @SaCheckLogin
    public Result<Object> updatePassword(@RequestBody @Validated UpdatePasswordParam updatePasswordParam) {
        log.info("用户密码修改请求");
        
        try {
            boolean result = frontUsersService.updatePassword(updatePasswordParam);
            if (result) {
                return Result.ok(null, "密码修改成功");
            } else {
                return Result.fail("密码修改失败，请稍后重试");
            }
        } catch (Exception e) {
            log.error("用户密码修改失败: {}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 获取用户详细信息
     * @return 用户详细信息
     */
    @ApiOperation(value = "获取用户详细信息", notes = "获取当前登录用户的订单数和电子票数")
    @GetMapping("/detail")
    @SaCheckLogin
    public Result<UserDetailDTO> getUserDetail() {
        log.info("获取用户详细信息请求");
        
        try {
            UserDetailDTO userDetail = frontUsersService.getCurrentUserDetail();
            return Result.ok(userDetail);
        } catch (Exception e) {
            log.error("获取用户详细信息失败: {}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
} 